new Vue({
  el: '#app',
  data() {
    return {
      form: {
        Article_Name: "",
        Category_SerialNumber: "",
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      checkboxGroup2: [],
      cities: ['本企业', '外企已发布', '外企'],
      Release: '0',
      formData: {},
      dialogFormVisible: false,
      Multistage: [{
        id: '',
        Article_SerialNumber: '',
        Unit_Price: '',
        Quantity: '',
      }],
      tableData: [{
      }],
      tablePic: [],
      Pic: [],
      loading: false,
      total: 1,
      dialogForm: false,
      title: '',
      multipleSelection: [],
      Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      Loading: false,
      coverLayerStyle: {
        transform: ""
      },
      coverLayerImg: {},
      coverLayerShow: false,
      mask: false,
      maskURL: '',
      topStyle: { transform: "" },
      r_img: {},
      topShow: false,
      rShow: false,
      imgDelet: false,//激活删除图片
      Province:[],
    }
  },
  mounted() {
    this.getList(this.form)
  },
  methods: {
    getData(){
      Province
      let _this = this
      $.ajax({
        type: 'POST',
        data:{
          Data:'article_province'
        },
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
        success: function (data) {
          _this.Province = data
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = '../../../' + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    onSerach() {				//查询方法
      this.form.page = 1;
      this.getList(this.form)
    },
    seeDetail(id,item, row) {
      if(!this.imgDelet||id==1){
        this.mask = true
        this.maskURL = DomainIMG + (id==1?item.Picture_URL:item.Picture_Url)
      }else if(id==0){
        $.ajax({
          url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
          type: 'POST',
          data:{
            Data:'article_pic',
            Primary_Key_Name:'Article_PicNumber',
            Primary_Key:item.Article_PicNumber,
            isNullEnterprise:true
          },
          success: function (res) {
            if (res.length == 1) {
              res[0].Row_Status = '1'
              $.ajax({
                url: DomainName + '/getEditAction?token=' + $.cookie('token'),
                type: 'POST',
                data:{Data:JSON.stringify({
                  Modify:{article_pic:res}
                })},
                success: function (res) {
                  if (res.Status === 'success') {
                    _this.$message.success("删操成功")
                    _this.handleChildren(row,false)
                  } else {
                    row.loading = false
                    _this.$message.error("操作失败，请稍后再试")
                  }
                },
                error: function () {
                  row.loading = false
                  _this.$message.error("删操作败，请稍后再试")
                },
              })
            } else {
              _this.$message.error("操作失败，请稍后再试")
            }
          },
          error: function () {
            _this.$message.error("删操作败，请稍后再试")
          },
          complete: function () { row.loading = false }
        })
        console.log(item);
      }
    },
    errorimg(item) {
      item.Picture_Url = '/error.png'
    },
    offMask() {
      this.mask = false
    },
    enterHandler() {
      // 层罩及放大空间的显示
      this.topShow = true;
      this.rShow = true;
    },
    // 鼠标移动函数
    moveHandler(event) {
      // 鼠标的坐标位置
      let x = event.offsetX;
      let y = event.offsetY;
      // 层罩的左上角坐标位置，并对其进行限制：无法超出原图区域左上角
      let topX = x - 100 < 0 ? 0 : x - 100;
      let topY = y - 100 < 0 ? 0 : y - 100;
      // 对层罩位置再一次限制，保证层罩只能在原图区域空间内
      if (topX > 200) {
        topX = 200;
      }
      if (topY > 200) {
        topY = 200;
      }
      // 通过 transform 进行移动控制
      this.topStyle.transform = `translate(${topX}px,${topY}px)`;
      this.r_img.transform = `translate(-${2 * topX}px,-${2 * topY}px)`;
    },
    // 鼠标移出函数
    outHandler() {
      // 控制层罩与放大空间的隐藏
      this.topShow = false;
      this.rShow = false;
    },
    addContract() {
      console.log("跳转")
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
      this.getList(this.form)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form)
    },
    getList(data) {
      let _this = this
      $.ajax({
        type: 'POST',
        data,
        url: DomainName + '/getArticleInfo3Action?token=' + $.cookie("token"),
        success: function (data) {
          _this.tableData = data.tableData;
          _this.tableData.forEach(item => {
            item.loading = false
          })
          _this.total = parseInt(data.limit)
          console.log(_this.tableData)
          _this.tableData.forEach(item => {
            item.srcList = []
            item.srcList.push(DomainIMG + item.Picture_URL)
          })
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = '../../../' + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      // $.ajax({
      //   type: 'POST',
      //   data: { Data: "article_pic" },
      //   url: DomainName + '/GoodsFileInquiry',
      //   success: function (data) {
      //     console.log(data)
      //     data.forEach(item => {
      //       item.Picture_Url = DomainName + item.Picture_Url.replace('https://hospital-one.essc.top', '')
      //     })
      //     _this.tablePic = data;
      //   },
      //   dataType: "json"
      // });
    },
    handlePreview(Article_SerialNumber) {
      this.Pic = []
      this.tablePic.filter(item => {
        return item.Picture_Type == '0' && item.Article_SerialNumber == Article_SerialNumber
      }).forEach(item => {
        this.Pic.push(item.Picture_Url)
      })
      console.log(this.Pic);
    },
    addEnterprise() {
      window.location.href = './Add.html'
    },
    // 查看详情
    showMember(Article_SerialNumber) {
      sessionStorage.setItem("Article_SerialNumber", Article_SerialNumber)
      window.location.href = './Edit.html?Article_SerialNumber=' + Article_SerialNumber
    },
    // editMember(Article_SerialNumber) {
    //   sessionStorage.setItem("Article_SerialNumber", Article_SerialNumber)
    //   window.location.href = './Edit.html?Article_SerialNumber=' + Article_SerialNumber
    // },
    // 下架
    deletMember(row) {
      const _this = this
      row.loading = true
      let detel = {};
      $.ajax({
        url: DomainName + '/ArticleInfo_Original',
        type: 'POST',
        data: { Data: row.Article_SerialNumber },
        success: function (res) {
          if (res.length == 1) {
            if (res[0].Row_Status == '0') {
              res[0].Row_Status = '1'
              detel = res[0];
              let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
              $.ajax({
                url: DomainName + '/GoodsFileEdit',
                type: 'POST',
                data,
                success: function (res) {
                  if (res.Status === 'success') {
                    row.loading = false
                    _this.$message.success("下架成功");
                    _this.getList(_this.form);
                  } else {
                    row.loading = false
                    _this.$message.error("操作失败，请稍后再试")
                  }
                },
                error: function () {
                  row.loading = false
                  _this.$message.error("操作失败，请稍后再试")
                },
                complete: function () { row.loading = false }
              })
            } else {
              res[0].Row_Status = '0'
              detel = res[0];
              let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
              $.ajax({
                url: DomainName + '/GoodsFileEdit',
                type: 'POST',
                data,
                success: function (res) {
                  if (res.Status === 'success') {
                    row.loading = false
                    _this.$message.success("上架成功");
                    _this.getList(_this.form);
                  } else {
                    row.loading = false
                    _this.$message.error("操作失败，请稍后再试")
                  }
                },
                error: function () {
                  row.loading = false
                  _this.$message.error("删操作败，请稍后再试")
                },
                complete: function () { row.loading = false }
              })
            }
          }
        }
      })

    },
    add_article_specifications() {
      this.Multistage.push({
        id: '',
        Article_SerialNumber: '',
        Unit_Price: '',
        Quantity: '',
      })
    },
    remove_article_specifications() {
      if (this.Multistage.length > 1) {
        this.Multistage.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    // 公布
    stateMember(row, i) {
      this.WholesaleById(row, i)
    },
    WholesaleById(row, i) {//通过主键获取批发价格单
      row.loading = true
      console.log(row)
      let _this = this
      if (row.Release_Type == '0' || i == 1) {
        $.ajax({
          url: DomainName + '/getArticleWholesale?token=' + $.cookie("token"),
          type: 'POST',
          data: {
            Article_SerialNumber: row.Article_SerialNumber,
          },
          success: function (res) {
            console.log(res.Data)
            if (res.Data.length > 0) {
              //查原值
              if (i == 0) {
                _this.getArticleInfoOriginal(row)
              } else {
                sessionStorage.setItem('GoodsData', JSON.stringify(res.Data[0]))
                sessionStorage.setItem('Goods', JSON.stringify(row))
                window.location.href = './SupplierGoods.html'
              }
            } else {
              _this.$confirm('还没有添加批发价格单, 是否添加?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
              }).then(() => {
                window.location.href = '../../../view/ShopManage/PriceManage/WholesalePrice/Add.html'
              }).catch(() => {
                _this.loading = false
                _this.$message({
                  type: 'info',
                  message: '已取消'
                });
              });
            }
          }
        })
      } else {
        _this.getArticleInfoOriginal(row)
      }
    },
    getArticleInfoOriginal(row) {//查原值
      let _this = this
      $.ajax({
        url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie("token"),
        type: 'POST',
        data: {
          Primary_Key_Name: 'Article_SerialNumber',
          Primary_Key: row.Article_SerialNumber,
          Data: 'article_info',
        },
        success: function (res) {
          console.log(res)
          if (res.length == 1) {
            if (res[0].Release_Type == '0') {
              res[0].Release_Type = '1'
              _this.Edit(res[0], row)
            } else {
              res[0].Release_Type = '0'
              _this.Edit(res[0], row)
            }
          } else {
            row.loading = false
          }
        }
      })
    },
    Edit(detel, row) {//修改
      let _this = this
      let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
      $.ajax({
        url: DomainName + '/GoodsFileEdit',
        type: 'POST',
        data,
        success: function (res) {
          if (res.Status === 'success') {
            _this.$message.success("修改成功");
            _this.getList(_this.form);
          } else {
            _this.$message.error("操作失败，请稍后再试")
          }
        },
        error: function () {
          row.loading = false
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          row.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    // 删除
    omitMember(Article_SerialNumber) {
      const _this = this
      let detel = {};
      $.ajax({
        url: DomainName + '/ArticleInfo_Original',
        type: 'POST',
        data: { Data: Article_SerialNumber },
        success: function (res) {
          if (res.length == 1) {
            res[0].Row_Status = '2'
            detel = res[0];
            let data = { Data: JSON.stringify({ Modify: { article_info: [detel] } }) };
            $.ajax({
              url: DomainName + '/GoodsFileEdit',
              type: 'POST',
              data,
              success: function (res) {
                if (res.Status === 'success') {
                  _this.$message.success("删除成功");
                  _this.getList(_this.form);
                } else {
                  _this.$message.error("操作失败，请稍后再试")
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              }
            })
          }
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 展开详细信息
    handleChildren(row,id=true) {
      _this = this
      if (!row.Children.article_specifications.length||!id) {
        console.log(row.Children.article_specifications.length)
        $.ajax({
          url: DomainName + '/getSpecificationsAction',
          type: 'POST',
          data: {
            Article_SerialNumber: row.Article_SerialNumber,
            token: $.cookie("token")
          },

          success: function (data) {
            console.log(data)
            row.Children.article_specifications = data.article_specifications
            row.Children.Swiper = data.Swiper
            row.Children.Detail = data.Detail
          },
          dataType: "json"
        });
      }

    }
  },
})

